<?php

// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------

namespace Admin\Controller;

use Admin\Controller\AdminController;

class StoreController extends AdminController {

    function _initialize() {
        parent::_initialize();
        $this->assign('sys', 'store'); // 数据集
    }

    //商家列表
    public function index() {
        if (IS_POST) {
            $company_name = I('user_name');
            $data['store_company_name'] = array('like', "%$company_name%");
        }
        $store = M('store'); // 实例化User对象
        $count = $store->where()->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 15); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show(); // 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $store->where($data)->order('store_id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('action', 'index');
        $this->display(); // 输出模板
    }

    //详情
    public function details() {
        $id = I('id');

        if (IS_POST) {
            $store_id = I('store_id');
            $joinin_state = I('joinin_state');
            $where['store_id'] = $store_id;
            $data['store_state'] = $joinin_state;
            $re = M('store')->where($where)->save($data);
            if ($re) {
                $this->success('店铺上线成功！');
            } else {
                $this->error('店铺审核失败');
            }
        }


        $store_joinin = M('store_joinin')->where(array('user_id' => $id))->find();
        $this->assign('res', $store_joinin);
        $this->assign('action', 'infos');
        $this->display();
    }

    public function add() {
        if (IS_POST) {
            $mobile = I("post.live_store_tel");
            $res = M('Users')->where(array('user_mobile' => $mobile))->find();
            if ($res) {
                $users = $res['user_id'];
                $u_data['user_name'] = $res['user_name'];
            } else {
                $u_data['user_name'] = I("post.title");
                $password = password(I("post.password"));
                $u_data['user_passwd'] = $password[0];
                $u_data['user_salt'] = $password[1];
                $u_data['user_mobile'] = $mobile;
                $u_data['user_mobile_bind'] = 1;
                $u_data['user_time'] = NOW_TIME;
                $users = M('Users')->data($u_data)->add();
            }
            $resources = M('Store')->where(array('member_id' => $users))->find();
            if (!$resources) {
                $s_data['store_name'] = I('live_store_name');
                $s_data['store_company_name'] = I('live_store_name');
                $s_data['live_store_name'] = I('live_store_name');
                $s_data['member_name'] = I('member_name');
                $s_data['live_store_tel'] = $mobile;
                $s_data['store_phone'] = $mobile;
                $s_data['member_id'] = $users;
                $s_data['store_state'] = 1;
                $s_data['store_time'] = time();
                $store = M('Store')->data($s_data)->add();
                if ($store) {
                    $where['user_id'] = $users;
                    $data['user_name'] = I("post.title");
                    $password = password(I("post.password"));
                    $data['user_passwd'] = $password[0];
                    $data['user_salt'] = $password[1];
                    $data['user_store_id'] = $store;
                    $data['user_group_id'] = 2;
                    $editor = M('Users')->where($where)->data($data)->save();
                    if ($editor === null) {
                        $this->error('操作失败');
                    } else {
                        $j_data['user_id'] = $users;
                        $j_data['user_name'] = $data['user_name'];
                        $j_data['company_name'] = $s_data['store_name'];
                        $j_data['store_name'] = $s_data['store_name'];
                        $j_data['contacts_name'] = $s_data['member_name'];
                        $j_data['contacts_phone'] = $s_data['live_store_tel'];
                        $j_data['bank_account_name'] = I("post.bank_account_name");
                        $j_data['bank_account_number'] = I("post.bank_account_number");
                        $j_data['bank_name'] = I("post.bank_name");
                        $store_joinin = M('Store_joinin')->data($j_data)->add();
                        if ($store_joinin) {
                            $this->success('操作成功', U('details', array('id' => $users)));
                        } else {
                            $this->error('操作失败');
                        }
                    }
                }
            } else {
                $this->error('该用户已注册过店铺啦');
            }
        }
        $this->assign('action', 'index');
        $this->display();
    }

    //编辑(修改)
    public function editor() {
        $where['user_id'] = I('id');
        $users = M('Users');
        $store = M('Store');
        $store_joinin = M('Store_joinin');
        $u = $users->where($where)->find();
        $this->assign('user', $u);
        $s = $store->where(array('member_id' => $where['user_id']))->find();
        $this->assign('store', $s);
        $j = $store_joinin->where($where)->find();
        $this->assign('store_joinin', $j);
        if (IS_POST) {
            $u_data['user_name'] = I("title");
            $u_data['user_mobile'] = I("live_store_tel");
            if (I('user_passwd')) {
                $pass = password(trim(I('user_passwd'), ''));
                $u_data['user_passwd'] = $pass[0];
                $u_data['user_salt'] = $pass[1];
            }
            $u_tf = $users->where($where)->data($u_data)->save();
            if ($u_tf !== null) {
                $s_data['store_name'] = I('live_store_name');
                $s_data['store_company_name'] = I('live_store_name');
                $s_data['live_store_name'] = I('live_store_name');
                $s_data['member_name'] = I('member_name');
                $s_data['live_store_tel'] = I("live_store_tel");
                $s_tf = $store->where(array('member_id' => $where['user_id']))->data($s_data)->save();
                if ($s_tf !== null) {
                    $j_data['user_name'] = I("title");
                    $j_data['company_name'] = I('live_store_name');
                    $j_data['store_name'] = I('live_store_name');
                    $j_data['contacts_name'] = I('member_name');
                    $j_data['contacts_phone'] = I("live_store_tel");
                    $j_data['bank_account_name'] = I("bank_account_name");
                    $j_data['bank_account_number'] = I("bank_account_number");
                    $j_data['bank_name'] = I("bank_name");
                    $j_tf = $store_joinin->where($where)->data($j_data)->save();
                    if ($j_tf !== null) {
                        $this->success('操作成功', U('details', array('id' => $where['user_id'])));
                    } else {
                        $this->error('操作失败');
                    }
                } else {
                    $this->error('操作失败');
                }
            } else {
                $this->error('操作失败');
            }
        }
        $this->assign('action', 'index');
        $this->display();
    }

    //放入下架ajax
    public function store_tuijian() {
        $where['store_id'] = I('store_id');
        $data['store_recommend'] = 1;
        $re = M('store')->where($where)->data($data)->save();
        if ($re) {
            $data['n'] = true;
        } else {
            $data['n'] = false;
        }
        $this->ajaxReturn($data);
    }

    //初始化重置密码
    public function store_chongzhi() {
        $where['user_id'] = I('user_id');
        $pass = password('123456');
        $data['user_passwd'] = $pass[0];
        $data['user_salt'] = $pass[1];
        $re = M('users')->where($where)->data($data)->save();

        if ($re) {
            $a['p'] = true;
        } else {
            $a['p'] = false;
        }
        $this->ajaxReturn($a);
    }

}
